package com.wzs.setting.web.controller;

import com.wzs.setting.domain.Author;
import com.wzs.setting.domain.Readr;
import com.wzs.setting.service.AuthorService;
import com.wzs.setting.service.ReaderService;
import com.wzs.utils.MD5Util;
import com.wzs.utils.PrintJson;
import com.wzs.vo.PaginationVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/author/")
public class authorController {

    @Resource
    private AuthorService authorService;
    @Resource
    private ReaderService readerService;

    @RequestMapping("login.do")
    public void Login(HttpServletResponse response, HttpServletRequest request,
                      String username, String password) {
        System.out.println("执行作者登录验证操作");

        //将密码的明文形式转换成MD5的密文形式
        password = MD5Util.getMD5(password);
        try {
            Author author = authorService.login(username, password);
            request.getSession().setAttribute("SESSION_USER", author);

            PrintJson.printJsonFlag(response, true);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            Map<String, Object> map = new HashMap<>();
            map.put("success", false);
            map.put("msg", msg);

            PrintJson.printJsonObj(response, map);
        }
    }

    @ResponseBody
    @RequestMapping("pageList.do")
    public PaginationVO<Readr> PageList(@RequestParam("pageNo") String pageNoStr,
                                        @RequestParam("pageSize") String pageSizeStr,
                                        String readerUserName) {
        int pageNo = Integer.parseInt(pageNoStr);
        int pageSize = Integer.parseInt(pageSizeStr);
        //计算出略过的记录数
        int skipCount = (pageNo - 1) * pageSize;
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageSize", pageSize);
        map.put("skipCount", skipCount);
        map.put("readerUserName", readerUserName);

        return authorService.pageList(map);
    }

    @RequestMapping("getReaderList.do")
    @ResponseBody
    public Readr getReaderList(String id) {
        System.out.println("进入到查询读者信息列表的操作");

        return authorService.getReaderList(id);
    }

    @RequestMapping("delete.do")
    @ResponseBody
    public boolean delete(HttpServletRequest request) {
        System.out.println("进入到删除读者信息的操作");

        String[] ids = request.getParameterValues("id");

        return authorService.delete(ids);
    }

    @RequestMapping("register.do")
    @ResponseBody
    public boolean register(Author author) {
        System.out.println("执行作者注册操作");
        return authorService.register(author);
    }

    @RequestMapping("loginOut.do")
    public String LoginOut(HttpServletRequest request) {

        request.getSession().removeAttribute("SESSION_USER");

        return "redirect:/index.html";
    }

    @RequestMapping("editReader.do")
    public String editReader(Model model, String id) {

        Readr readr = authorService.getReaderList(id);
        model.addAttribute("r",readr);

        return "author/editReader";
    }

    @RequestMapping("updateReader.do")
    @ResponseBody
    public boolean update(Readr readr) {

        return readerService.updateReader(readr);
    }

}
